package com.hz.web.controller;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hz.web.entity.GsHtmlEntity;
import com.hz.web.service.IgsHtmlService;
import com.hz.web.util.Constant;
import com.hz.web.util.ParmsEntity;
import com.hz.web.util.UtilMethods;

@Controller
@RequestMapping("GsController")
public class GsController {

	@Resource
	private IgsHtmlService service;
	
	
	/**
	 * 根据公示类型查询信息(前端首页展示)
	 * @param entity
	 * @return
	 */
	@RequestMapping("queryGsHtmlQian")
	@ResponseBody
	public Map<String, Object> queryGsHtmlQian(ParmsEntity entity){
		Map<String, Object> result = new HashMap<String, Object>();//结果返回map
		Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
		int ckType =entity.getCkType();
		int page = entity.getPage();
		int rows = entity.getRows();
		String state = entity.getState();
		parmsMap.put("ckType", ckType);//公示类型 
		if(page!=0 && rows!=0){
			int index = (page-1)*rows;
			int end =rows; 
			parmsMap.put("index", index);
			parmsMap.put("end", end);
			parmsMap.put("state", state);
		}
		//根据参数map查询出的富文本编辑的结果集合
		List<GsHtmlEntity> list = new ArrayList<GsHtmlEntity>();
		list =service.queryGsHtml(parmsMap);
		result.put("rows", list);
		return result;
	}
	
	
	/**
	 * 跳转到 不显示列表的 富文本页面
	 * @return
	 */
	@RequestMapping("toHtmlManage")
	public String toHtmlManage(){
		return "html_manage";
	} 
	
	/**
	 * 修改富文本信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("updateGsHtml")
	@ResponseBody
	public Map<String, Object> updateGsHtml (GsHtmlEntity entity){
		String title = entity.getTitle();
		if("CKEditorHtml".equals(title)){
			entity.setCreateTime(new Date());
			entity.setState(Constant.CKSTATS1);//默认是显示的
			entity.setId("");// 用类型作为查询条件使用
		}else{
			entity.setCkType(0);//用id作为查询条件使用  正常修改是不会修改  富文本类型的
		}
		Map<String, Object> result = new HashMap<String, Object>();//结果返回map
		int row =-1;
		try {
			System.out.println(entity.getId());
			 row =service.updateGsHtml(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("row", row);
		return result;
	}
	
	/**
	 * 向富文本表里插入信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("addGsHtml")
	@ResponseBody
	public Map<String, Object> addGsHtml(GsHtmlEntity entity){
		Map<String, Object> result = new HashMap<String, Object>();//结果返回map
		int row =-1;
		try {
			entity.setState(Constant.CKSTATS1);//默认是显示的
			 row = service.addGsHtml(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("row", row);
		return result;
	}
	
	/**
	 * 根据富文本id 或者  富文本类型 ckType 查询富文本信息
	 */
	@RequestMapping("queryHtmlById")
	@ResponseBody
	public Map<String, Object> queryHtmlById(GsHtmlEntity gsHtml){
		Map<String, Object> result = new HashMap<String, Object>();//结果返回map
		Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
		String id = gsHtml.getId();
		int gsType = gsHtml.getCkType();
		if(id!=null&&id!=""){
			parmsMap.put("id", id);
		}
		if(gsType !=0){
			parmsMap.put("gsType", gsType);
		}
		String state="";
		state = gsHtml.getState();
		parmsMap.put("state", state);
		
		List<GsHtmlEntity> html = new ArrayList<GsHtmlEntity>();
		GsHtmlEntity entity =new GsHtmlEntity();
		html=service.queryGsHtml(parmsMap);
		if(html.size()>0){
			entity = html.get(0);
			String htmlName="";
			htmlName=UtilMethods.getNameByHtmlType(entity.getCkType());
			entity.setTypeName(htmlName);
		}
		result.put("entity",entity);
		return result;
	}
	
	
	/**
	 * 跳转到富文本页面
	 * @return
	 */
	@RequestMapping("toAddUpdatehtml")
	public String toAddUpdatehtml(){
		return "gshtml";
	}
	
	/**
	 * 根据富文本类型查询富文本信息
	 * @param entity
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("queryGsHtml")
	@ResponseBody
	public Map<String, Object> queryGsHtml(ParmsEntity entity,String sort,String inputVal) throws UnsupportedEncodingException{
		if(inputVal!=null && !"".equals(inputVal.trim())){
			//inputVal = URLDecoder.decode(URLDecoder.decode(inputVal));
			inputVal=URLDecoder.decode(URLDecoder.decode(inputVal, "utf-8"),"utf-8");
		}
		Map<String, Object> result = new HashMap<String, Object>();//结果返回map
		Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
		int gsType =entity.getCkType();
		int page = entity.getPage();
		int rows = entity.getRows();
		String state = entity.getState();
		/*if(gsType==0){
			return result;
		}*/
		if(inputVal!=null && !"".equals(inputVal.trim())){
			parmsMap.put("inputVal", "%"+inputVal.trim()+"%");
		}
		if(state!=null && !"".equals(state.trim())){
			parmsMap.put("state", state.trim());
		}
		parmsMap.put("gsType", gsType);//富文本类型   判断是哪个功能的富文本信息
		parmsMap.put("sort", sort);
		if(page!=0 && rows!=0){
			int index = (page-1)*rows;
			int end =rows; 
			parmsMap.put("index", index);
			parmsMap.put("end", end);
			int count = 0;
			count = service.queryGsHtmlCount(parmsMap);
			result.put("total", count);
		}
		
		//根据参数map查询出的富文本编辑的结果集合
		List<GsHtmlEntity> list = new ArrayList<GsHtmlEntity>();
		list =service.queryGsHtml(parmsMap);
		String htmlName="";
		htmlName=UtilMethods.getNameByHtmlType(entity.getCkType());
		for (GsHtmlEntity gsHtml : list) {
			gsHtml.setTypeName(htmlName);
		}
		result.put("htmlName", htmlName);
		result.put("rows", list);
		
		return result;
	}

	
	/**
	 * 根据部门ID查询对应公示信息
	 * @param entity
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("selectGsHtml")
	@ResponseBody
	public Map<String, Object> selectGsHtml(GsHtmlEntity entity,int page ,int rows){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			
			List<GsHtmlEntity> ls = service.selectGsHtml(entity, page, rows);
			
			resultMap.put("rows", ls);
			int count = service.selectGsHtmlCount(entity);
			resultMap.put("total", count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	} 
}
